package context;

import java.util.Calendar;
import java.util.HashMap;

import org.apache.log4j.Logger;

public class UserContext {

	public static UserContext instance = new UserContext();
	private UserContext(){};
	
	private Logger log = Logger.getLogger(UserContext.class);
	
	HashMap<String, Long> userData = new HashMap<String, Long>();
	
	public void addUser(String name)
	{
		Calendar now = Calendar.getInstance();
		long time = now.getTimeInMillis();
		
		userData.put(name, time);
		log.info("Adding user : " + name + " time: " + time);
	}
	
	public boolean isStillValid(String name)
	{
		if (userData.containsKey(name))
		{
			long timeNow = Calendar.getInstance().getTimeInMillis();
			long userTime = userData.get(name);
			
			if (timeNow - userTime > 1200000)
			{
				//Timed out
				log.warn("User timed out : " + name);
				userData.remove(name);
				return false;
			}
		}
		else
		{  // not in list.
			log.warn("User not in list : " + name);
			return false; 
		}
		
		return true;
	}
	
	
}
